package com.jiuyin.dianjing.ui.activity.setting;

import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jiuyin.dianjing.R;
import com.jiuyin.dianjing.ui.activity.base.BaseActivity;
import com.jiuyin.dianjing.ui.activity.webview.WebViewContainer;
import com.jiuyin.dianjing.util.DataCleanManagerUtil;

import butterknife.BindView;
import butterknife.OnClick;

import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_TITLE;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_URL;
import static com.jiuyin.dianjing.constant.AppConstant.URL_FW;

public class SettingActivity extends BaseActivity {
    @BindView(R.id.tv_cache)
    TextView mCache;
    @BindView(R.id.rl_cache)
    RelativeLayout mRlCache;

    @Override
    protected int setLayoutId() {
        return R.layout.activity_setting;
    }

    @Override
    protected void initData() {


    }

    @Override
    protected void initView() {
        setTitle(R.string.my_setting);
        String cacheSize = DataCleanManagerUtil.getTotalCacheSize(this);
        mCache.setText(cacheSize);
    }

    @OnClick({R.id.tv_account, R.id.tv_quit, R.id.rl_cache, R.id.tv_user_agreement, R.id.tv_about_company})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_account:
                goTargetActivity(SettingSecurityActivity.class);
                break;
            case R.id.tv_quit:
                quitAndLogin();
                break;
            case R.id.rl_cache:
                showDialog();
                break;
            case R.id.tv_user_agreement: {
                String url = URL_FW;
                Bundle bundle = new Bundle();
                bundle.putString(KEY_URL, url);
                bundle.putString(KEY_TITLE, getString(R.string.setting_user_agreement));
                goTargetActivity(WebViewContainer.class, bundle);
                break;
            }
            case R.id.tv_about_company: {
                goTargetActivity(AboutJiuYinActivity.class);
                break;
            }
        }
    }

    /**
     * 提示框
     */
    private void showDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(SettingActivity.this);
        builder.setTitle(R.string.dialog_title_tips)
                .setMessage(R.string.dialog_content_cache_tips)
                .setPositiveButton(getString(R.string.dialog_confirm_tips),
                        (dialog, whichButton) -> {
                            DataCleanManagerUtil.clearAllCache(getApplicationContext());
                            String cacheSize = DataCleanManagerUtil.getTotalCacheSize(this);
                            mCache.setText(cacheSize);
                        }
                )
                .setNegativeButton(getString(R.string.dialog_cancel_tips), null
                )
                .setCancelable(false);
        builder.show();
    }
}
